﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Threading;
using System.Data;

public partial class SYS_POPEDOM_Sys_Param_Edit : BasePage
{
    public static string stub = "当[参数类型]为“选项”时，[参数选项]内容格式： 1:选择项内容@2:选择项内容@3:选择项内容...";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindSubSystemComb();
            if (Request.QueryString["ObjId"] == "Add")
            {
                ViewState["Operator"] = "Add";
            }else
            {
                ViewState["Operator"] = "Edit";
                ViewState["PARAMETER_ID"] = Request.QueryString["PARAMETER_ID"];
                this.exttxtf_PARAMETER_ID.Text = ViewState["PARAMETER_ID"].ToString();
                this.exttxtf_PARAMETER_ID.ReadOnly = true;

                //获取数据库记录

                DataTable dt = CommonLib.BLL.SYS_PARAMETER.GetList(" PARAMETER_ID ='" + ViewState["PARAMETER_ID"].ToString() + "'").Tables[0];
                if (dt.Rows.Count > 0)
                {
                    this.exttxtf_PARAMETER_NAME.Text = dt.Rows[0]["PARAMETER_NAME"].ToString();
                    this.exttxtf_PARAMETER_NOTE.Text = dt.Rows[0]["PARAMETER_NOTE"].ToString();
                    this.exttxtf_DeptID.Text=dt.Rows[0]["DEPT_ID"].ToString();
                    this.exttrig_DEPT_Name.Text=(dt.Rows[0]["DEPT_Name"].ToString());
                    this.exttare_REMARK.Text = dt.Rows[0]["REMARK"].ToString();
                    this.exttare_PARAMETER_OPTION.Text = dt.Rows[0]["PARAMETER_OPTION"].ToString();
                    if (dt.Rows[0]["SHOW_FLAG"].ToString() == "1")
                    {
                        this.extrdio_1.Checked = true;
                        this.extrdio_2.Checked = false;
                    }
                    else
                    {
                        this.extrdio_1.Checked = false;
                        this.extrdio_2.Checked = true;
                    }
                    this.extcmbo_PARAMETER_TYPE.SetValue(dt.Rows[0]["PARAMETER_TYPE"]);
                    this.extcmbo_SUB_SYSTEM.SetValue(dt.Rows[0]["SUB_SYSTEM"]);
                    this.extcmbo_SUB_SYSTEM.SetRawValue(dt.Rows[0]["SUB_SYSTEM_NAME"]);
                }
            }

            BuildTreeRoot();
            
        }  
    }

    //构建部门树根节点
    private void BuildTreeRoot()
    {
        DataTable tmpDt = CommonLib.BLL.SYS_DEPT.GetListSub(" dept_id='00' and  del_flag =0 ", "dept_id").Tables[0];
        if (tmpDt.Rows.Count == 1)
        {
            Ext.Net.AsyncTreeNode root = new Ext.Net.AsyncTreeNode();
            root.Text = tmpDt.Rows[0]["dept_name"].ToString();
            root.NodeID = tmpDt.Rows[0]["dept_id"].ToString();
            root.Expanded = true;
            root.Checked = ThreeStateBool.False;
            root.IconCls = "no-icon";
            if (tmpDt.Rows[0]["leaf"].ToString() == "0")
                root.Leaf = true;
            this.TreePanel1.Root.Add(root);
        }
        //Dictionary<string, object> agc = new Dictionary<string, object>();
        //this.btnExpand.FireEvent("click", agc);
    }

    //加载下属节点
    [DirectMethod]
    public string NodeLoad(string nodeID)
    {
        Ext.Net.TreeNodeCollection nodes = new Ext.Net.TreeNodeCollection();
        if (!string.IsNullOrEmpty(nodeID))
        {
            DataTable TreeData = CommonLib.BLL.SYS_DEPT.GetListSub(" del_flag =0 and ltrim(rtrim(dept_id)) like '" + nodeID.Trim() + "__' ", "dept_id").Tables[0];
            for (int i = 0; i < TreeData.Rows.Count; i++)
            {
                AsyncTreeNode asyncNode = new AsyncTreeNode();
                asyncNode.Text = TreeData.Rows[i]["dept_name"].ToString();
                asyncNode.NodeID = TreeData.Rows[i]["dept_id"].ToString();
                asyncNode.Checked = ThreeStateBool.False;
                asyncNode.IconCls = "no-icon";
                if (TreeData.Rows[i]["leaf"].ToString() == "0")
                    asyncNode.Leaf = true;
                nodes.Add(asyncNode);
            }
        }
        return nodes.ToJson();
    }

    //参数类型选项变化
    protected void OnParamTypeChange(object sender, DirectEventArgs e)
    {
        if (this.extcmbo_PARAMETER_TYPE.SelectedItem.Value == "2")
        {
            Ext.Net.Notification.Show(new NotificationConfig// Notification.Config
            {

                AlignCfg = new NotificationAlignConfig//Notification.AlignConfig
                {
                    ElementAnchor = AnchorPoint.Left,
                    TargetAnchor = AnchorPoint.Right,
                    OffsetX = 2,
                    OffsetY = 0,
                    El = this.extcmbo_PARAMETER_TYPE.ClientID
                },
                ShowFx = new SlideIn { Anchor = AnchorPoint.Left },
                HideFx = new Ghost { Anchor = AnchorPoint.Left },
                Height = 30,
                Width=600,
                HideDelay = 10000,
                BodyStyle = "boder:0px",
                Html = stub
            });
        }
    }

    //绑定子系统下拉框
    private void BindSubSystemComb()
    {
        DataTable dt = CommonLib.BLL.SYS_SUBSYSTEM.GetAllList("SUB_SYSTEM").Tables[0];
        this.Store1.DataSource = dt;
        this.Store1.DataBind();

        //this.extcmbo_SUB_SYSTEM.Items.Insert(0, new Ext.Net.ListItem("", "0"));
        //extcmbo_SUB_SYSTEM.SelectedIndex=0;
    }

    //保存系统参数
    protected void SaveSysParam(object sender, DirectEventArgs e)
    {
        bool clsValue = false;
        bool deptUpdate = false;
        if (this.exttxtf_PARAMETER_ID.Text.Trim() == "")
        {
            X.Msg.Alert("系统提示", "参数编号不能为空！").Show();
            return;
        }
        else
        {
            if (ViewState["Operator"].ToString() == "Add")
            {
                if (CommonLib.BLL.SYS_PARAMETER.GetRecorderCount(" PARAMETER_ID ='" + this.exttxtf_PARAMETER_ID.Text.Trim() + "'") > 0)
                {
                    X.Msg.Alert("系统提示", "参数编号已存在请重新编号！").Show();
                    return;
                }
            }
        }

        if (this.exttxtf_PARAMETER_NAME.Text.Trim() == "")
        {
            X.Msg.Alert("系统提示", "参数名称不能为空！").Show();
            return;
        }

        CommonLib.Model.SYS_PARAMETER oModel = new CommonLib.Model.SYS_PARAMETER();

        if (ViewState["Operator"].ToString() == "Edit")
        {
            oModel = CommonLib.BLL.SYS_PARAMETER.GetModel(ViewState["PARAMETER_ID"].ToString());
        }
        oModel.PARAMETER_ID=this.exttxtf_PARAMETER_ID.Text.Trim();
        oModel.PARAMETER_NAME = this.exttxtf_PARAMETER_NAME.Text.Trim();
        oModel.PARAMETER_NOTE = this.exttxtf_PARAMETER_NOTE.Text.Trim();

        if (oModel.PARAMETER_TYPE != int.Parse(this.extcmbo_PARAMETER_TYPE.SelectedItem.Value))
        {
            clsValue = true;
        }
        else
        {
            if (oModel.PARAMETER_OPTION != this.exttare_PARAMETER_OPTION.Text)
                clsValue = true;
        }
        oModel.PARAMETER_TYPE = int.Parse(this.extcmbo_PARAMETER_TYPE.SelectedItem.Value);
        oModel.PARAMETER_OPTION = this.exttare_PARAMETER_OPTION.Text;
        if (this.extcmbo_SUB_SYSTEM.SelectedItem.Value != "")
            oModel.SUB_SYSTEM = short.Parse(this.extcmbo_SUB_SYSTEM.SelectedItem.Value);
        else
            oModel.SUB_SYSTEM = 0;
        if (extrdio_1.Checked)
            oModel.SHOW_FLAG = 1;
        else
            oModel.SHOW_FLAG = 0;
        oModel.REMARK = this.exttare_REMARK.Text;
        oModel.DEL_FLAG = 0;
        if (oModel.DEPT_ID != exttxtf_DeptID.Value.ToString())
            deptUpdate = true;
        oModel.DEPT_ID = exttxtf_DeptID.Value.ToString();

        try
        {
            if (ViewState["Operator"].ToString() == "Add")
            {
                CommonLib.BLL.SYS_PARAMETER.AddSysParam(oModel);
            }
            else
            {
                CommonLib.BLL.SYS_PARAMETER.UpdateSysParam(oModel,clsValue,deptUpdate);
            }

            X.Msg.Alert("系统提示", "保存成功!").Show();
        }
        catch
        {
            X.Msg.Alert("系统提示", "保存失败!").Show();
        }
        

    }


}
